
package com.netease.xone.adapter;

import java.util.ArrayList;
import java.util.List;

import protocol.XoneCallBack;
import protocol.XoneService;
import protocol.meta.Source;
import protocol.meta.Subject;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import com.netease.xone.R;
import com.netease.xone.itemview.RenderSourceView;
import com.netease.xone.itemview.RenderSubjectView;

public class ExpandSubjectAdapter extends BaseExpandableListAdapter {

    private List<Subject> mList;
    private Context mContext;

    public ExpandSubjectAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<Subject>();
        XoneService.getInstance().addListener(mCallBack);
    }

    public void addAll(List<Subject> list) {
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public void removeGroup(Subject subject){
        mList.remove(subject);
        notifyDataSetChanged();
    }

    @Override
    public Source getChild(int groupPosition, int childPosition) {
        return mList.get(groupPosition).sourceList.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return mList.get(groupPosition).sourceList.get(childPosition).sourceId;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        if (convertView == null) {
            View v = View.inflate(mContext, R.layout.view_source_item, null);
            RenderSourceView render = new RenderSourceView(v);
            v.setTag(render);
            convertView = v;
        }
        RenderSourceView render = (RenderSourceView)convertView.getTag();
        render.renderView(mContext, getChild(groupPosition, childPosition), childPosition == 0,
                isLastChild);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        int childCount = mList.get(groupPosition).sourceList == null ? 0
                : mList.get(groupPosition).sourceList.size();
        return childCount;
    }

    @Override
    public Subject getGroup(int groupPosition) {
        return mList.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return mList.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return mList.get(groupPosition).subjectId;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        if (convertView == null) {
            View v = View.inflate(mContext, R.layout.view_expand_subject_item, null);
            RenderSubjectView render = new RenderSubjectView(v);
            v.setTag(render);
            convertView = v;
        }
        RenderSubjectView render = (RenderSubjectView)convertView.getTag();
        render.renderView(mContext, getGroup(groupPosition));
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public void removeListener(){
        XoneService.getInstance().removeListener(mCallBack);
    }

    XoneCallBack mCallBack = new XoneCallBack() {

        @Override
        public void onSubscribe(int transactionId, int[] subjectIdAndSourceId) {
            // 有订阅信息，需要更新订阅状态
            int subjectId = subjectIdAndSourceId[0];
            int sourceId = subjectIdAndSourceId[1];
            // 更新订阅状态
            for (Subject subject : mList) {
                if (subject.sourceList == null || subject.sourceList.size() == 0)
                    continue;// 忽略没有资讯源的主题
                if (subjectId > 0) {
                    // subjectId > 0,表明是订阅全部
                    if (subjectId == subject.subjectId) {
                        // 更新该主题下的所有资讯源
                        for (Source source : subject.sourceList) {
                            source.isSubscribed = true;
                            source.subscribeCount++;
                            subject.subscribeCount++;
                            subject.mySubCount++;
                        }
                        notifyDataSetChanged();
                        break;// 找到之后就没必要继续查找了
                    }
                } else {
                    // 订阅单个资讯源
                    for (Source source : subject.sourceList) {
                        if (sourceId == source.sourceId) {
                            // 更新该资讯源
                            source.isSubscribed = true;
                            source.subscribeCount++;
                            subject.mySubCount++;
                            subject.subscribeCount++;
                            notifyDataSetChanged();
                            break;// 找到之后就没必要继续查找了
                        }
                    }
                }
            }
        };

        @Override
        public void onUnsubscribe(int transactionId, int sourceId) {
            // 更新订阅状态
            for (Subject subject : mList) {
                if (subject.sourceList == null || subject.sourceList.size() == 0)
                    continue;// 忽略没有资讯源的主题
                // 订阅单个资讯源
                for (Source source : subject.sourceList) {
                    if (sourceId == source.sourceId) {
                        // 更新该资讯源
                        source.isSubscribed = false;
                        source.subscribeCount--;
                        subject.mySubCount--;
                        subject.subscribeCount--;
                        notifyDataSetChanged();
                        break;// 找到之后就没必要继续查找了
                    }
                }
            }
        };
    };

}
